package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author cisimu
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //1.查询护理计划
        NursingPlan plan = this.getById(id);
        //2.查询护理项目计划中间表
        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //3.数据封装
        NursingPlanVo planVo = BeanUtil.toBean(plan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVoList = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        planVo.setProjectPlans(nursingProjectPlanVoList);
        return planVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        boolean save = this.save(nursingPlan);
        if (!save) {
            throw new RuntimeException("护理计划主表插入失败");
        }

        //2.新增护理项目计划中间表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(CollectionUtils.isNotEmpty(projectPlans)){
            List<NursingProjectPlan> projectPlans1 = projectPlans.stream().filter(nursingProject -> nursingProject.getProjectId() != null)
                    .peek(nursingProject -> nursingProject.setPlanId(Long.valueOf(nursingPlan.getId()))).toList();
            if(!projectPlans1.isEmpty()){
                boolean b = nursingProjectPlanService.saveBatch(projectPlans1);
                if(!b){
                    throw new RuntimeException("护理项目批量插入失败");
                }
            }
        }

        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.修改护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        this.updateById(nursingPlan);
        //2.修改护理项目计划中间表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
            //2.1删除护理项目计划中间表
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            //2.2批量添加护理项目计划中间表
        if (CollectionUtils.isNotEmpty(projectPlans)) {
            List<NursingProjectPlan> validPlans = projectPlans.stream()
                    .filter(p -> p.getProjectId() != null && p.getProjectId() > 0)
                    .peek(p -> p.setPlanId(Long.valueOf(nursingPlan.getId())))
                    .toList();

            if (!validPlans.isEmpty()) {
                boolean saved = nursingProjectPlanService.saveBatch(validPlans);
                if (!saved) {
                    throw new RuntimeException("护理项目重新插入失败");
                }
            }
        }

        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return removeByIds(Arrays.asList(ids))==true?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Integer id)
    {
        //1.删除护理计划
        boolean b = this.removeById(id);
        //2.删除护理项目计划中间表
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,id));
        return b?1:0;
    }
}
